package io.renren.modules.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.member.dao.StandardTotalDao;
import io.renren.modules.member.entity.ExamEntity;
import io.renren.modules.member.entity.MemberEntity;
import io.renren.modules.member.entity.StandardTotalEntity;
import io.renren.modules.member.enums.AchivTotalJudgeEnums;
import io.renren.modules.member.service.StandardTotalService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import java.util.Objects;

@Service
public class StandardTotalServiceImpl extends ServiceImpl<StandardTotalDao, StandardTotalEntity> implements StandardTotalService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String type = (String) params.get("type");

        IPage<StandardTotalEntity> page = this.page(
                new Query<StandardTotalEntity>().getPage(params),
                new QueryWrapper<StandardTotalEntity>()
                        .eq(StringUtils.isNotBlank(type), "type", type)
        );
        return new PageUtils(page);
    }

    public String getLevel(ExamEntity entity, MemberEntity memberEntity) {
        if (Objects.isNull(memberEntity.getType())) {
            return null;
        }
        StandardTotalEntity totalEntity = baseMapper.findByType(memberEntity.getType());
        if (Objects.isNull(totalEntity)) {
            return null;
        }
        // BMI
        if (!StringUtils.equals(entity.getShapeLevel(), "1")) {
            return AchivTotalJudgeEnums.BUJIGE.getCode();
        }
        // 男：引体向上；女：曲臂悬垂
        if (StringUtils.equals(memberEntity.getSex(), "1")) {
            if (entity.getCrankArmScore().compareTo(totalEntity.getAvgScore()) == -1) {
                return AchivTotalJudgeEnums.BUJIGE.getCode();
            }
        } else {
            if (entity.getCrankArmScore().compareTo(totalEntity.getAvgScore()) == -1) {
                return AchivTotalJudgeEnums.BUJIGE.getCode();
            }
        }
        // 俯卧撑
//        if (entity.getPushUpScore().compareTo(totalEntity.getAvgScore()) == -1) {
//            return AchivTotalJudgeEnums.BUJIGE.getCode();
//        }
        // 仰卧起坐
        if (entity.getSitUpScore().compareTo(totalEntity.getAvgScore()) == -1) {
            return AchivTotalJudgeEnums.BUJIGE.getCode();
        }
        // 蛇形跑
        if (entity.getSnakeRunScore().compareTo(totalEntity.getAvgScore()) == -1) {
            return AchivTotalJudgeEnums.BUJIGE.getCode();
        }
        // 中长跑
        if (entity.getMiddleRunScore().compareTo(totalEntity.getAvgScore()) == -1) {
            return AchivTotalJudgeEnums.BUJIGE.getCode();
        }

        BigDecimal totalScore = entity.getTotalScore();
        if (totalScore.compareTo(totalEntity.getG7Score()) != -1) {
            return AchivTotalJudgeEnums.TEYIJI.getCode();
        }
        if (totalScore.compareTo(totalEntity.getG6Score()) != -1) {
            return AchivTotalJudgeEnums.TEERJI.getCode();
        }
        if (totalScore.compareTo(totalEntity.getG5Score()) != -1) {
            return AchivTotalJudgeEnums.TESANJI.getCode();
        }
        if (totalScore.compareTo(totalEntity.getG4Score()) != -1) {
            return AchivTotalJudgeEnums.YOUXIU.getCode();
        }
        if (totalScore.compareTo(totalEntity.getG3Score()) != -1) {
            return AchivTotalJudgeEnums.LIANGHAO.getCode();
        }
        if (totalScore.compareTo(totalEntity.getG2Score()) != -1) {
            return AchivTotalJudgeEnums.JIGE.getCode();
        }
        if (totalScore.compareTo(totalEntity.getG1MinScore()) == -1) {
            return AchivTotalJudgeEnums.BUJIGE.getCode();
        }
        return AchivTotalJudgeEnums.BUJIGE.getCode();
    }
}
